package ru.jexp.rcp.supply.n.form;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;

import ru.jexp.rcp.supply.beans.ek.n.RowFormDialog;
import ru.jexp.rcp.supply.img.JExpImageLink;

public abstract class RowFormView extends AbstractFormPart implements IRowFormView
{
	public abstract void createContent(FieldFactory factory, ScrolledForm form);

	private RowFormDialog dialog;
	private String title;

	public RowFormView(Shell shell, String title)
	{
		this.title = title;
		this.dialog = new RowFormDialog(shell, this);
	}

	@Override
	public void initialize(IManagedForm form)
	{
		super.initialize(form);
		createContent(new FieldFactory(form.getToolkit()), form.getForm());
	}

	@Override
	public void createView(Mode mode)
	{
		dialog.setTitle(mode.getName() + ": " + title);
		dialog.setImageDescriptor(getImageDescriptor(mode));
		dialog.create();
	}
	
	protected ImageDescriptor getImageDescriptor(Mode mode)
	{
		if (Mode.ADD == mode)
		{
			return JExpImageLink.getImageDescriptor(JExpImageLink.NEW);
		}
		if (Mode.EDIT == mode)
		{
			return JExpImageLink.getImageDescriptor(JExpImageLink.EDIT);
		}
		if (Mode.CHOOSE == mode)
		{
			return JExpImageLink.getImageDescriptor(JExpImageLink.LOOKUP);
		}
		return null;
	}

	@Override
	public StatusResult showView()
	{
		return dialog.open() == Window.OK ? StatusResult.OK : StatusResult.CANCEL;
	}
}
